# import numpy as np
import matplotlib.pyplot as plt
import os
import time as time


# 定义电影类
class Movie:
    def __init__(self, no, mingcheng, piaofang, pingfen, date, day, film, preem):
        self.no = no  # 电影ID
        self.mingcheng = mingcheng  # 电影名称
        self.piaofang = piaofang  # 电影票房
        self.pingfen = pingfen  # 电影评分
        self.date = date  # 放映日期
        self.day = day  # 放映星期
        self.film = film  # 是否为电影
        self.preem = preem  # 是否为首映


# 定义电影管理类
class MovieMS:
    movies = []

    def __init__(self):
        pass

    def menu(self):

        while 1:
            print("-------------------------------------------------------------------------------")
            print("1-从文件读出数据保存到列表中-（输入数字1）")
            print("2-显示前10条数据和后10条数据-（输入数字2）")
            print("3-对数据进行汇总统计-（输入数字3）")
            print("4-把统计数据以CSV形式保存到文件中-（输入数字4）")
            print("5-查看影票房和日期走势图和电影票房和星期走势图-（输入数字5）")
            print("0-退出-（输入数字0）")
            print("-------------------------------------------------------------------------------")
            function_num = int(input("请选择功能!"))
            print("-------------------------------------------------------------------------------")

            if function_num == 1:
                print("-------------------------------------------------------------------------------")
                print("欢迎使用文件读出数据保存到列表中功能")
                print("-------------------------------------------------------------------------------")
                self.do_read()

            elif function_num == 2:
                print("-------------------------------------------------------------------------------")
                print("欢迎使用显示前10条数据和后10条数据功能")
                print("-------------------------------------------------------------------------------")
                self.do_pb10display()

            elif function_num == 3:
                print("-------------------------------------------------------------------------------")
                print("欢迎使用数据进行汇总统计功能")
                print("-------------------------------------------------------------------------------")
                self.do_statistic()

            elif function_num == 4:
                print("-------------------------------------------------------------------------------")
                print("欢迎使用统计数据以CSV形式保存到文件中功能")
                print("-------------------------------------------------------------------------------")
                self.do_add_save()

            elif function_num == 5:
                print("-------------------------------------------------------------------------------")
                print("欢迎使用查看影票房和日期走势图和电影票房和星期走势图功能")
                print("-------------------------------------------------------------------------------")
                self.do_chart()

            elif function_num == 0:
                return

    # 文件读出数据保存到列表中
    def do_read(self):
        path = "data/movies_data.txt"

        if not os.path.exists:
            print("-------------------------------------------------------------------------------")
            print("文件不存在哟!")
            print("-------------------------------------------------------------------------------")

        else:
            with open(path, "r", encoding="utf-8") as f:
                # 不读取第一行数据
                index = 0

                for line in f:
                    print(line)

                    movie = line.split(";")
                    movie[-1] = movie[-1].split("\n")[0]

                    print(movie)

                    if index == 0:
                        index += 1

                    else:
                        one = Movie(movie[0], movie[1], movie[2], movie[3], movie[4], movie[5], movie[6], movie[7])
                        self.movies.append(one)
                        index += 1

    # 显示前10条数据和后10条数据
    def do_pb10display(self):
        length = len(self.movies)

        if length >= 10:
            a = 10

            print("前十条数据如下:")
            print("--------------------------------------------------")

            while a:
                for i in self.movies:
                    print(i.no, i.mingcheng, i.piaofang, i.pingfen, i.date, i.day, i.film, i.preem)
                    a -= 1
                    if a <= 0:
                        break
            time.sleep(2)

            print("后十条数据如下:")
            print("-------------------------------------------------------------------------------")

            while length:
                for i in self.movies:
                    length -= 1

                    if length <= 10:
                        print(i.no, i.mingcheng, i.piaofang, i.pingfen, i.date, i.day, i.film, i.preem)

            print("-------------------------------------------------------------------------------")
            time.sleep(2)

        else:
            print("很遗憾，数据不足十条!请添加更多的数据!")
            time.sleep(2)
            print("目前的数据如下:")
            time.sleep(1)
            print("-------------------------------------------------------------------------------")

            for i in self.movies:
                print(i.no, i.mingcheng, i.piaofang, i.pingfen, i.date, i.day, i.film, i.preem)

            print("-------------------------------------------------------------------------------")
            time.sleep(2)

    # 数据进行汇总统计
    def do_statistic(self):
        while 1:
            print("-------------------------------------------------------------------------------")
            print("1-统计电影总数-(输入数字1)")
            print("2-统计总票房-(输入数字2)")
            print("3-统计平均票房-(输入数字3)")
            print("4-统计平均评分-(输入数字4)")
            print("0-退出-(输入数字0)")
            print("-------------------------------------------------------------------------------")
            number = int(input("您的选择是?"))
            print("-------------------------------------------------------------------------------")

            path = "data/movies_data.txt"  # 文件地址

            if number == 1:
                data = len(self.movies)

                print("-------------------------------------------------------------------------------")
                print("目前记录的电影共计", " ", data, " ", "部")
                print("-------------------------------------------------------------------------------")
                time.sleep(2)

            elif number == 2:
                if not os.path.exists(path):
                    print("-------------------------------------------------------------------------------")
                    print("文件不存在哟!")
                    print("-------------------------------------------------------------------------------")

                else:
                    piaofang = 0
                    number = 0

                    with open(path, "r", encoding="utf-8") as f:
                        for i in f:
                            data = i.split(";")

                            if number == 0:  # 不读取第一行数据
                                number += 1
                                continue

                            piaofang += int(data[2])

                    print("-------------------------------------------------------------------------------")
                    print("目前电影的总票房为", " ", piaofang, " ", "场次")
                    print("-------------------------------------------------------------------------------")
                    time.sleep(2)

            elif number == 3:
                if not os.path.exists(path):
                    print("-------------------------------------------------------------------------------")
                    print("文件不存在哟!")
                    print("-------------------------------------------------------------------------------")

                else:
                    piaofang = 0
                    number = 0

                    with open(path, "r", encoding="utf-8") as f:
                        for i in f:
                            data = i.split(";")

                            if number == 0:
                                number += 1
                                continue

                            data_1 = int(data[2])
                            length = len(self.movies)
                            piaofang += data_1 / length

                    print("-------------------------------------------------------------------------------")
                    print("目前电影的平均票房为", " ", int(piaofang), " ", "场次")
                    print("-------------------------------------------------------------------------------")
                    time.sleep(2)

            elif number == 4:
                if not os.path.exists(path):
                    print("-------------------------------------------------------------------------------")
                    print("文件不存在哟!")
                    print("-------------------------------------------------------------------------------")

                else:
                    pingfen = 0
                    number = 0

                    with open(path, "r", encoding="utf-8") as f:
                        for i in f:
                            data = i.split(";")

                            if number == 0:
                                number += 1
                                continue

                            data_1 = data[3]
                            length = len(self.movies)
                            pingfen += float(data_1) / length

                    print("-------------------------------------------------------------------------------")
                    print("目前电影的平均评分为", " ", pingfen, " ", "分")
                    print("-------------------------------------------------------------------------------")
                    time.sleep(2)

            elif number == 0:
                break

    # 把统计数据以CSV形式保存到文件中
    def do_add_save(self):
        while 1:
            print("-------------------------------------------------------------------------------")
            print("1-把统计数据以CSV形式保存到文件中-(输入数字1)")
            print("0-退出-(输入数字0)")
            number = int(input("您的选择是?"))
            print("-------------------------------------------------------------------------------")

            if number == 1:
                print("-------------------------------------------------------------------------------")
                print("输入格式如下:")
                print("xx(电影ID) xx(电影名称加英文版双引号，为了好看) xx(电影票房) x.x(电影评分) x月x日(放映日期) 周x(放映星期) 1/0(是否为电影-) 1/0(是否为当日首映)")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                no = input("请输入该电影ID")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                mingcheng = input("请输入该电影名称")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                piaofang = input("请输入该电影票房")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                pingfen = input("请输入该电影评分")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                date = input("请输入该电影放映日期")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                day = input("请输入该电影放映星期")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                film = input("请输入该电影是否为电影-（输入1为是）-（输入0为否）")
                print("-------------------------------------------------------------------------------")
                time.sleep(1)
                print("-------------------------------------------------------------------------------")
                preem = input("请输入该电影是否为当日首映-（输入1为是）-（输入0为否）")
                print("-------------------------------------------------------------------------------")

                movie = Movie(no, mingcheng, piaofang, pingfen, date, day, film, preem)
                self.movies.append(movie)

                path = "data/movies_data.txt"

                if not os.path.exists:
                    print("-------------------------------------------------------------------------------")
                    print("文件不存在哟!")
                    print("-------------------------------------------------------------------------------")

                else:
                    with open(path, "a", encoding="utf-8") as f:
                        line = no + ";" + mingcheng + ";" + piaofang + ";" + pingfen + ";" + date + ";" + day + ";" \
                               + film + ";" + preem + "\n"
                        f.write(line)

                        print("-------------------------------------------------------------------------------")
                        print("写入成功!")
                        print("-------------------------------------------------------------------------------")

            elif number == 0:
                break

    # 查看影票房和日期走势图和电影票房和星期走势图
    def do_chart(self):
        plt.figure()

        # 为了显示中文，指定默认字体
        plt.rcParams['font.sans-serif'] = ['SimHei']

        # path = "data/movies.txt"  # 若地址失效，请启用备用地址，若备用地址也失效······寄!
        path = "C:\\Users\\Administrator\\PycharmProjects\\python-teach\\Python教学\\21级34班\\教学示例\\data\\movies.txt"

        if not os.path.exists(path):
            print("-------------------------------------------------------------------------------")
            print("文件不存在哟!")
            print("-------------------------------------------------------------------------------")

        else:
            print("-------------------------------------------------------------------------------")
            print("1-根据输入ID查询-(输入数字1)")
            print("2-总览-(输入数字2)")
            print("0-退出-(输入数字0)")
            print("-------------------------------------------------------------------------------")
            number = int(input("您的选择是?"))
            print("-------------------------------------------------------------------------------")

            piaofang_1 = []
            piaofang_2 = []
            week = []
            date = []
            a = 7
            b = 7

            with open(path, "r", encoding="utf-8") as f:
                for i in f:
                    data = i.split(",")

                    if a > 0:
                        data_1 = int(data[3])

                        piaofang_1.append(data_1)
                        date.append(data[2])
                        week.append(data[4])

                        a -= 1

                    elif b > 0:
                        data_2 = int(data[3])

                        piaofang_2.append(data_2)

                        b -= 1

                if number == 1:
                    print("-------------------------------------------------------------------------------")
                    number = int(input("请输入电影ID(偷个懒，实不相瞒只有两部)"))
                    print("-------------------------------------------------------------------------------")
                    if number == 1:
                        # 图1.1_第一部电影(折线图)-票房/星期
                        plt.subplot(221)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“铃芽户缔”票房记录")
                        x1_1 = week
                        y1_1 = piaofang_1
                        plt.plot(x1_1, y1_1, 'b', lw=3)

                        # 图1.2第一部电影(柱状图)-票房/星期
                        plt.subplot(222)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“铃芽户缔”票房记录")
                        x1_2 = week
                        y1_2 = piaofang_1
                        plt.bar(x=x1_2, height=y1_2)

                        # 图1.3_第一部电影(折线图)-票房/日期
                        plt.subplot(223)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“铃芽户缔”票房记录")
                        x1_3 = date
                        y1_3 = piaofang_1
                        plt.plot(x1_3, y1_3, 'b', lw=3)

                        # 图1.4第一部电影(柱状图)-票房/日期
                        plt.subplot(224)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“铃芽户缔”票房记录")
                        x1_4 = date
                        y1_4 = piaofang_1
                        plt.bar(x=x1_4, height=y1_4)

                        plt.show()

                    elif number == 2:
                        # 图2.1_第二部电影(折线图)-票房/星期
                        plt.subplot(221)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“夏日幽灵”票房纪录")
                        x2_1 = week
                        y2_1 = piaofang_2
                        plt.plot(x2_1, y2_1, 'r', lw=3)

                        # 图2.2第二部电影(柱状图)-票房/星期
                        plt.subplot(222)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“夏日幽灵”票房纪录")
                        x2_2 = week
                        y2_2 = piaofang_2
                        plt.bar(x=x2_2, height=y2_2)

                        # 图2.3_第二部电影(折线图)-票房/日期
                        plt.subplot(223)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“夏日幽灵”票房纪录")
                        x2_3 = date
                        y2_3 = piaofang_2
                        plt.plot(x2_3, y2_3, 'r', lw=3)

                        # 图2.4第二部电影(柱状图)-票房/日期
                        plt.subplot(224)
                        plt.xlabel("日期")
                        plt.ylabel("票房")
                        plt.title("“夏日幽灵”票房纪录")
                        x2_4 = date
                        y2_4 = piaofang_2
                        plt.bar(x=x2_4, height=y2_4)

                        plt.show()

                elif number == 2:
                    # 图1.1_第一部电影(折线图)-票房/星期
                    plt.subplot(241)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“铃芽户缔”票房记录")
                    # Python自己带的arange函数生成也可以
                    x1_1 = week  # np.arange(7)  # 创建数组x
                    # np.random.seed(500)  # 随机种子
                    y1_1 = piaofang_1  # np.random.randint(20, size=(10,))  # 创建数组y，size是生成随机数的个数
                    plt.plot(x1_1, y1_1, 'b', lw=3)  # 绘制折线图，lw是线条粗细

                    # 图2.1_第二部电影(折线图)-票房/星期
                    plt.subplot(245)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“夏日幽灵”票房纪录")
                    # Python自己带的arange函数生成也可以
                    x2_1 = week  # np.arange(7)  # 创建数组x
                    # np.random.seed(500)  # 随机种子
                    y2_1 = piaofang_2  # np.random.randint(20, size=(10,))  # 创建数组y，size是生成随机数的个数
                    plt.plot(x2_1, y2_1, 'r', lw=3)  # 绘制折线图，lw是线条粗细

                    # 图1.2第一部电影(柱状图)-票房/星期
                    plt.subplot(242)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“铃芽户缔”票房记录")
                    x1_2 = week
                    y1_2 = piaofang_1
                    plt.bar(x=x1_2, height=y1_2)

                    # 图2.2第二部电影(柱状图)-票房/星期
                    plt.subplot(246)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“夏日幽灵”票房纪录")
                    x2_2 = week
                    y2_2 = piaofang_2
                    plt.bar(x=x2_2, height=y2_2)

                    # 图1.3_第一部电影(折线图)-票房/日期
                    plt.subplot(243)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“铃芽户缔”票房记录")
                    # Python自己带的arange函数生成也可以
                    x1_3 = date  # np.arange(7)  # 创建数组x
                    # np.random.seed(500)  # 随机种子
                    y1_3 = piaofang_1  # np.random.randint(20, size=(10,))  # 创建数组y，size是生成随机数的个数
                    plt.plot(x1_3, y1_3, 'b', lw=3)  # 绘制折线图，lw是线条粗细

                    # 图2.3_第二部电影(折线图)-票房/日期
                    plt.subplot(247)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“夏日幽灵”票房纪录")
                    # Python自己带的arange函数生成也可以
                    x2_3 = date  # np.arange(7)  # 创建数组x
                    # np.random.seed(500)  # 随机种子
                    y2_3 = piaofang_2  # np.random.randint(20, size=(10,))  # 创建数组y，size是生成随机数的个数
                    plt.plot(x2_3, y2_3, 'r', lw=3)  # 绘制折线图，lw是线条粗细

                    # 图1.4第一部电影(柱状图)-票房/日期
                    plt.subplot(244)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“铃芽户缔”票房记录")
                    x1_4 = date
                    y1_4 = piaofang_1
                    plt.bar(x=x1_4, height=y1_4)

                    # 图2.4第二部电影(柱状图)-票房/日期
                    plt.subplot(248)
                    plt.xlabel("日期")
                    plt.ylabel("票房")
                    plt.title("“夏日幽灵”票房纪录")
                    x2_4 = date
                    y2_4 = piaofang_2
                    plt.bar(x=x2_4, height=y2_4)

                    plt.show()

                elif number == 0:
                    pass


if __name__ == '__main__':
    print("如果有人把文件里的内容改了，那就直接寄了")

    time.sleep(2)

    start = MovieMS()
    start.menu()
